import 'package:abc/controller/clear_cache_controller.dart';
import 'package:abc/view/CustomAppBar.dart';
import 'package:abc/view/GradientButton.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class ClearCachePage extends GetView<ClearCacheController> {
  const ClearCachePage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
        if (!Get.isRegistered<ClearCacheController>()) {
      Get.lazyPut(() => ClearCacheController());
    }
    return Scaffold(
      backgroundColor: const Color(0xFF0E0F26), // 深色背景
      appBar: CustomAppBar(
        title: "清理缓存",
        onBack: () {
          Get.back();
        },
      ),
      body: _buildContent(),
    );
  }
  
  Widget _buildContent() {
    return Column(
        mainAxisAlignment: MainAxisAlignment.start,
        
        children: [
          // 高266的Container
          Container(
            height: 266,
         
            decoration: BoxDecoration(
              color: const Color(0x33959595),
              borderRadius: BorderRadius.circular(15),
            ),
            child: Center(
              child: Obx(() {
                return Stack(
                  alignment: Alignment.center,
                  children: [
                    // 渐变圆环
                    Transform.rotate(
                      angle: controller.rotationAngle.value,
                      child: CustomPaint(
                        size: const Size(164, 164),
                        painter: GradientRingPainter(
                          progress: controller.clearStatus.value == 2 ? 100.0 : 0.0, // 清理完成时显示100%
                        ),
                      ),
                    ),
                    // 内存大小文本
                    Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      crossAxisAlignment: CrossAxisAlignment.end,
                      children: [Text(
                      controller.valueBytes(controller.cacheSize.value),
                      style: const TextStyle(
                        color: Colors.white,
                        fontSize: 40,
                        fontWeight: FontWeight.bold,
                        height: 1
                      ),
                    ),
                    Text(
                      controller.formatBytes(controller.cacheSize.value),
                      style: const TextStyle(
                        color: Colors.white,
                        fontSize: 20,
                        fontWeight: FontWeight.w600,
                         height: 1
                      ),
                    ),],)
                  ],
                );
              }),
            ),
          ),
          const SizedBox(height: 30),
          // GradientButton按钮
          Obx(() {
            return GradientButton(
              text: controller.clearStatus.value == 0 
                  ? '立即清理' 
                  : controller.clearStatus.value == 1 
                      ? '清理中...' 
                      : '清理完成',
              onPressed: controller.clearStatus.value == 1 
                  ? null // 清理中时禁用按钮
                  : () {
                      controller.startClearCache();
                      // 开始旋转动画
                      _startRotationAnimation();
                  },
              
              margin: const EdgeInsets.symmetric(horizontal: 0.0),
            );
          }),
        ],
      
    ).paddingOnly(left: 16,right: 16,top: 58);
  }
  
  // 开始旋转动画
  void _startRotationAnimation() {
    // 使用Timer.periodic来持续更新旋转角度
    Future.delayed(Duration.zero, () {
      if (controller.isSpinning.value) {
        controller.updateRotation();
        _startRotationAnimation(); // 递归调用以持续更新
      }
    });
  }
}

// 渐变圆环绘制器
class GradientRingPainter extends CustomPainter {
  final double progress;
  
  GradientRingPainter({required this.progress});

  @override
  void paint(Canvas canvas, Size size) {
    final center = Offset(size.width / 2, size.height / 2);
    final radius = size.width / 2;
    final strokeWidth = 5.0;
    
    // 创建渐变
    final gradient = LinearGradient(
      colors: [
        const Color(0xFFA877F4),
        const Color(0xFF5650D6),
      ],
    );
    
    // 创建画笔
    final paint = Paint()
      ..shader = gradient.createShader(
        Rect.fromCircle(center: center, radius: radius),
      )
      ..style = PaintingStyle.stroke
      ..strokeWidth = strokeWidth
      ..strokeCap = StrokeCap.round;
    
    // 绘制完整的渐变圆环
    canvas.drawCircle(center, radius, paint);
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return true;
  }
}